package view.listeners;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import model.HostAddress;
import model.Subnet;

import controller.Controller;

import view.ToolBar;
import view.components.IpAddressTable;
import view.components.NetworkTree;
import view.dialogs.AddHostDialog;
import view.dialogs.AddIpByListDialog;
import view.dialogs.AddSubnetDialog;
import view.dialogs.ChooseOptionDialog;
import view.dialogs.DeleteDialog;
import view.dialogs.HostInfoDialog;
import view.dialogs.SubnetInfoDialog;

public class ToolBarListener extends MouseAdapter {

	public void setTree(NetworkTree tree) {
		this.tree = tree;
	}

	public void setTable(IpAddressTable table) {
		this.table = table;
	}

	private NetworkTree tree;
	private IpAddressTable table;

	@Override
	public void mouseClicked(MouseEvent e) {
		JButton clickedButton = (JButton) e.getComponent();
		DefaultMutableTreeNode selectedNode = tree.getSelectedNode();
		System.out.println(clickedButton.getToolTipText());
		int selectedRow = table.getSelectedRow();
		if (clickedButton.getToolTipText().equalsIgnoreCase(
				"Host/Subnetz/IP löschen")) {

			delete(selectedNode, selectedRow);

		}
		if (clickedButton.getToolTipText().equalsIgnoreCase(
				"Host/Subnetz/IP hinzufügen")) {

			add(selectedNode);

		}
		if (clickedButton.getToolTipText().equalsIgnoreCase("Informationen")) {
			information(selectedNode, selectedRow);
		}

	}

	private void information(DefaultMutableTreeNode selectedNode,
			int selectedRow) {
		if (selectedNode.getLevel() == 1) {
			hostInformation();
		}

		if (selectedNode.getLevel() == 2 && selectedRow == -1) {
			subnetInformation();
		}
		if (selectedRow != -1) {
			System.out.println("IP-Adress Info");
		}
	}

	private void subnetInformation() {
		Subnet subnet = tree.getSubnetFromAddress(tree
				.getSelectedNode().toString());
		Controller.getInstance().setCurrentSubnet(subnet);
		new SubnetInfoDialog("Info");
	}

	private void hostInformation() {
		HostAddress host = tree.getHostFromAddress(tree
				.getSelectedNode().toString());
		Controller.getInstance().setCurrentHost(host);
		new HostInfoDialog("Info");
	}
	private void add(DefaultMutableTreeNode selectedNode) {
		if (selectedNode.getLevel() == 0) {
			new AddHostDialog("Host hinzufügen");
		}
		if (selectedNode.getLevel() == 1) {
			new AddSubnetDialog("Subnetz hinzufügen");
		}
		if (selectedNode.getLevel() == 2) {
			new ChooseOptionDialog("IP-Adresse hinzufügen");
		}
	}

	private void delete(DefaultMutableTreeNode selectedNode, int selectedRow) {
		if (selectedNode != null && selectedRow == -1) {
			removeFromTree(selectedNode);
		}

		if (selectedNode != null && selectedRow != -1) {
			removeFromTable();
		}
		if (selectedNode == null && selectedRow == -1) {
			System.out.println("Nüscht");
		}
	}

	private void removeFromTable() {
		new DeleteDialog("IP-Adresse löschen?", "Wollen Sie wirklich löschen?");
		// table.setIpAddressEmpty(table.getSelectedIPAdress());
	}

	private void removeFromTree(DefaultMutableTreeNode selectedNode) {
		if (selectedNode.getLevel() == 1) {
			new DeleteDialog("Host-Adresse löschen?",
					"Wollen Sie wirklich löschen?");
		}
		if (selectedNode.getLevel() == 2) {
			new DeleteDialog("Subnetz löschen?", "Wollen Sie wirklich löschen?");
		}
		// tree.removeRow();
	}
	
	

}